package com.cjk.console.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

@Service
public class MqttServiceImpl implements MqttService {

	@Autowired
	private MqttPahoMessageHandler mqttPahoMessageHandler;

	@Override
	public void publish(String topic, Object mqttMessage) {
		Message<String> message = MessageBuilder.withPayload(JSONObject.toJSONString(mqttMessage)) //
				.setHeader(MqttHeaders.TOPIC, topic) //
				.build();
		mqttPahoMessageHandler.handleMessage(message);
	}

}
